//
//  WLComposePictureView.swift
//  WLWeibo
//
//  Created by Devil on 16/8/17.
//  Copyright © 2016年 Wiliam. All rights reserved.
//

import UIKit

private let WLComposePictureViewCellId = "WLComposePictureViewCellId"
class WLComposePictureView: UICollectionView {

    var imageList:[UIImage] = [UIImage]()
    
    var closure:(() -> ())?
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func addImages(image: UIImage){
        //添加到数组中
        imageList.append(image)
        //刷新UI
        reloadData()
    }
    
    
    private func setupUI(){
    
        backgroundColor = RandomColor()
        //注册Cell
        registerClass(WLComposePictureViewCell.self, forCellWithReuseIdentifier: WLComposePictureViewCellId)
        //设置代理
        dataSource = self
        delegate = self
        //设置layout
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        //cell间距
        let cellMargin: CGFloat = 5
        //cell宽高
        let cellHW: CGFloat = (SCREENW - 20 - 2*cellMargin)/3
        //设置itemSize
        layout.itemSize = CGSize(width: cellHW, height: cellHW)
        //设置垂直和水平间距
        layout.minimumInteritemSpacing = cellMargin
        layout.minimumLineSpacing = cellMargin
    
    }

}

extension WLComposePictureView: UICollectionViewDataSource,UICollectionViewDelegate{

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        let count = imageList.count
        if count == 0 || count == 9 {
            return count
        }
        
        return imageList.count + 1
        
    }
    
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(WLComposePictureViewCellId, forIndexPath: indexPath) as! WLComposePictureViewCell
        
        return cell
        
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        collectionView.deselectItemAtIndexPath(indexPath, animated: true)
        if imageList.count == indexPath.item {
            //执行闭包
            closure?()
        }
        
    }

}
//自定义Cell
class WLComposePictureViewCell: UICollectionViewCell {
    //供外界调用
    var image: UIImage?{
        //赋值
        didSet{
            //加图片
            if image == nil {
                bgImageView.image = UIImage(named: "compose_pic_add")
                bgImageView.highlightedImage = UIImage(named:"compose_pic_add_highlighted")
            }else{
                bgImageView.image = image
                //解决Cell复用
                bgImageView.highlightedImage = nil
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置视图
    private func setupUI(){
        backgroundColor = RandomColor()
        //添加控件
        contentView.addSubview(bgImageView)
        //添加约束
        bgImageView.snp_makeConstraints { (make) in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
    
    }
    //懒加载控件
    private lazy var bgImageView: UIImageView = UIImageView()
    
    
}
